<?php
class YWebUser extends CWebUser {
	private $data = null;
	
	public function login($identity, $duration=0){
		parent::login($identity, $duration);
		self::_init_user_data($identity->user);
	}

	protected function beforeLogout(){
		Yii::app()->cache->delete('duoec_user_data_' . self::getId());
		return parent::beforeLogout();
	}
	
	public function __get($name){
		if(parent::hasState($name)){
			return parent::__get($name);
		}
		if(empty($this->data)){
			self::_init_user_data();
		}
		if($this->data!==null && array_key_exists($name, $this->data))
			return $this->data[$name];
		else
			return null;
	}
	
	public function hasState($key){
		self::_init_user_data();
		
		if(isset($this->data[$key]))
			return true;
		else
			return parent::hasState($key);
	}
	
	/**
	 * 初始化用户数据
	 */
	private function _init_user_data($user = null){
		if(!empty($this->data))return;
		
		if($user == null && parent::getIsGuest()){
			//如果是游客时
			$this->data = array(
				'isGuest' 			=> true,
				'id'				=> 0,
				'name'				=> '游客',
				'roles'				=> null,			//角色名称(数组)
				'permissions'		=> null,		//用户所有权限
			);
			return;
		}
		$this->data = Yii::app()->cache->get('duoec_user_data_' . self::getId());
		if($this->data !== false){
			return;
		}
		
		//加载数据
		$this->data = array(
			'id'				=> $this->getId(),
			'name'				=> empty($user) ? $this->getName() : $user['user_name'],
			'isGuest'			=> false,
			'roles'				=> self::get_my_roles(),			//角色名称(数组)
			'permissions'		=> self::get_my_permissions(),		//用户所有权限
		);
		Yii::app()->cache->set('duoec_user_data_' . self::getId(), $this->data);
	}
	
	private $role_array = array();
	private function get_my_roles(){
		//获取用户角色
		if(empty($this->role_array)){
			$roles = Yii::app()->authManager->getRoles(self::getId());
			foreach($roles as $role_name=>$role){
				$this->role_array[] = $role_name;
			}
		}
		return $this->role_array;
	}
	
	/**
	 * 获取我的权限
	 */
	private function get_my_permissions(){
		$ps = array();
		//加载总是允许项
		Yii::import('srbac.components.Helper');
		$alway_alloweds = Helper::findModule('srbac')->getAlwaysAllowed();
		foreach($alway_alloweds as $al){
			$ps[strtolower($al)] = true;
		}
		
		//加载登录后允许项
		$authorizeAllowed = Helper::findModule('srbac')->getAuthorityAllowed();
		foreach ($authorizeAllowed as $item){
			$ps[strtolower($item)] = true;
		}
		
		//加载角色
		$roles = $this->get_my_roles();
		$parents = array();
		foreach ($roles as $role){
			$ps[strtolower($role)] = true;
			$parents[] = $role;
		}
		
		//加载角色权限
		$authManager = Yii::app()->authManager;
		while ($parents !== array()) {
			$items = $authManager->getItemChildren($parents);
			$parents = array();
			foreach ($items as $item) {
				$name = $item->getName();
				$ps[strtolower($name)] = true;
				$parents[] = $name;
			}
		}
		return $ps;
	}
	
	/**
	 * 判断某个用户是否有某个权限，$route可以是一个操作权限也可以是一个角色或者是一个任务（各项概念请参考本应用的权限管理模块）
	 * @param unknown $item_name
	 */
	public function v($item_name){
		$this->_init_user_data();
		if(!isset($this->data['permissions'])){
			return false;
		}
		
		return array_key_exists(strtolower($item_name), $this->data['permissions']);
	}
	
	public function loginRequired(){
		$app = Yii::app();
		$request = $app->getRequest();
		if(($url = $this->loginUrl)!==null) {
			$return_url = '/' . $request->pathInfo;
			if(!empty($request->queryString)){
				$return_url .= '?' . $request->queryString;
			}
			$url .= '?return_url=' . urlencode($return_url);
		}else{
			$url = '/';
		}
		$request->redirect($url);
	}
}

?>